/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.security;

import com.legendshop.framework.model.GrantedFunction;
import com.legendshop.oa.constants.FunctionEnum;
import com.legendshop.util.AppUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;
import java.util.Date;

/**
 * 扩展的Spring security用户
 * 
 */
public class UserDetail extends User {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -5898134878028341628L;

	/** The functions. */
	private final Collection<GrantedFunction> functions;

	/** The user id. */
	private final Long userId;

	// 头像图片路径
	private final String portraitPic;

	/** 登录的IP地址 **/
	private String ipAddress;

	/** 部门Id */
	private Integer deptId;
	
	/** 真实名称 */
	private String realName;


	public void setPassChangeDate(Date passChangeDate) {
		this.passChangeDate = passChangeDate;
	}

	private Date passChangeDate; //密码修改时间

	private Integer changePass; //是否需要修改密码， 如果等于1则自动导向到修改密码页面

	/**
	 * 在做并发控制时, 以userId来区分一个唯一的用户.
	 */
	public boolean equals(Object object) {
		if (object instanceof UserDetail) {
			if (this.userId.equals(((UserDetail) object).getUserId()))
				return true;
		}
		return false;
	}

	public int hashCode() {
		return this.userId.hashCode();
	}

	public UserDetail(Long userId, String username, String password, String portraitPic, boolean enabled, boolean accountNonExpired,
			boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities, Collection<GrantedFunction> functions,
			Integer deptId,String realName, Date passChangeDate, Integer changePass) {
		super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
		this.functions = functions;
		this.userId = userId;
		this.portraitPic = portraitPic;
		this.deptId = deptId;
		this.realName = realName;
		this.passChangeDate = passChangeDate;
		this.changePass = changePass;
	}

	/**
	 * Gets the functions.
	 * 
	 * @return the functions
	 */
	public Collection<GrantedFunction> getFunctions() {
		return functions;
	}
	
	/**
	 * 检查是否有这个权限
	 * @return
	 */
	public boolean hasFunction(FunctionEnum function) {
		if(AppUtils.isBlank(functions)) {
			return false;
		}
		for (GrantedFunction grantedFunction : functions) {
			if(grantedFunction.getFunction().equals(function.value())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Gets the user id.
	 * 
	 * @return the user id
	 */
	public Long getUserId() {
		return userId;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	/**
	 * 返回copy的新对象
	 * 
	 * @return
	 */
	public Object newUser() {
		return new UserDetail(this.userId, this.getUsername(), this.getPassword(), this.portraitPic, this.isEnabled(), this.isAccountNonExpired(),
				this.isCredentialsNonExpired(), this.isAccountNonLocked(), this.getAuthorities(), this.functions, this.deptId,this.realName, this.passChangeDate, this.changePass);
	}

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public String getPortraitPic() {
		return portraitPic;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public Date getPassChangeDate() {
		return passChangeDate;
	}

	public Integer getChangePass() {
		return changePass;
	}

}
